<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}
if(!checkSession())
{
			function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}
?>

<?php

if(!isset($BLOG))
{
	include('inc/functions_blog.php');
	$NEWS = array();
	if(isset($_REQUEST['nid']))
	{
		$NEWS = getNewsById($_REQUEST['nid']);
	}
	else
	{
		header('Location: index.php?action=news');
		exit;
	}
}

if(!isset($_REQUEST['commentid']))
	{

	}
	else
	{
	$sql="DELETE FROM comments WHERE comment_id = " . $_REQUEST['commentid'] .  "";
	if(!$result = $db->sql_query($sql)) 
			{
					printError("SQL ERROR", mysql_error());
			}
	// Add -1 to user_posts
			$sql2 = "UPDATE users SET user_posts = user_posts-1 WHERE user_id =" . $_REQUEST['userid'] . "";
			if(!$result2 = $db->sql_query($sql2))
			{
				printError("SQL ERROR", mysql_error());
			}
		$id = $_REQUEST['nid'];
		echo"<meta http-equiv=\"refresh\" content=\"0;url=index.php?action=news&sub=show&nid=$id\" />";
	}

$dateformat = $CONFIG['date_format'];
$dateformat_long = $CONFIG['date_format_long'];

if(checkSession() && ($USER->getId() == $NEWS['user_id']))
{
	$U_PROFILE = $USER;
	$url_to_post = "index.php?action=news&sub=show&nid=" . $NEWS['id'];
}
else
{
	$U_PROFILE = new User('id', $NEWS['user_id']);
	$url_to_post = "index.php?action=news&sub=show&id=" . $U_PROFILE->getId() . "&nid=" . $NEWS['id'];
}

$comments = getNewsCommentsSort(" WHERE post_type='news' AND post_id='" . $NEWS['id'] . "' ",$USER->getSortComments());
$comments_text0 = str_replace('VAR1', count($comments), $LANG['post_comments0']);
$comments_text1 = str_replace('VAR1', count($comments), $LANG['post_comments1']);
$comments_text2 = str_replace('VAR1', count($comments), $LANG['post_comments2']);

$comments_per_page = 15;
$start = 0;

if(checkSession())
		{
			$comments_per_page = $USER->getCommentsPerPage();
		}
		if(isset($_REQUEST['p']))
		{
			$start = ($_REQUEST['p'] * $comments_per_page) - $comments_per_page;
		}
		
		$end = $start + $comments_per_page;
		if($end > count($comments))
		{
			$end = count($comments);
		}

?>
<script type="text/javascript">
function quote_comment(textarea_id, comment_id)
{
	var element = document.getElementById(textarea_id);
	var name = document.getElementById('name_' + comment_id);
	var date = document.getElementById('date_' + comment_id);
	var comment = document.getElementById('comment_' + comment_id);
	element.value += '[quote name=' + name.innerHTML.trim() + ' date=' + date.value + ']' + comment.innerHTML.trim() + '[/quote]';
}

function delete_comment(id, number, userid)
{
	var confirmation = confirm('<?php echo translate("confirm_delete_comment"); ?>');
	if(confirmation == true)
	{
		window.location = window.location + "&delete&commentid=" + id + "&userid=" +  userid;
	}
}
</script>

<!-- Show single blog post START-->
<?php
if(isset($_REQUEST['nid']))
{
	if(isset($_POST['toggle_commenting']) && (checkSession() && ($USER->getId() == $NEWS['user_id'])) )
	{
		if($NEWS['disable_comment'] == 1)
		{
			$sql = "UPDATE " . NEWS_TABLE . " SET news_disable_comment='0' WHERE news_id='" . $NEWS['id'] . "'";
		}
		else
		{
			$sql = "UPDATE " . NEWS_TABLE . " SET news_disable_comment='1' WHERE news_id='" . $NEWS['id'] . "'";
		}
		
		if(!$result = $db->sql_query($sql))
		{
			printError('SQL_ERROR', mysql_error());
		}
		else
		{
			$NEWS = getNewsById($_REQUEST['nid']);
		}
	}
	
	$blog_user_id = $U_PROFILE->getId();
	
// Counter
$ip = $_SERVER['REMOTE_ADDR']; 
$category = "news_post";
$page = $_REQUEST['nid']; 
$timenow = mktime();

//Check that the IP is not already listed for the current page 
$viewer_check_query = "SELECT ip, category, page, time FROM counter WHERE ip = '$ip' AND category = '$category' AND page = '$page'"; 
$viewer_check_result = mysql_query($viewer_check_query); 
$viewer_check_numrows = mysql_num_rows($viewer_check_result); 
while($row = mysql_fetch_assoc($viewer_check_result)) {
    $time = $row['time'];
}
    //If numrows is equal to zero, then the user is new 
    if($viewer_check_numrows == 0){ 
        //Add the new entry 
        $viewer_new_query = "INSERT INTO counter (ip, category, page, time) VALUES 
        ('$ip', '$category', '$page', '$timenow')"; 
        $viewer_new_result = mysql_query($viewer_new_query); 
    } 
	//Timeout of 1 hours before same ip can add one new hit
	elseif((time() - $time) > 3600 ){ 
        //Add the new entry 
        $viewer_new_query = "INSERT INTO counter (ip, category, page, time) VALUES 
        ('$ip', '$category', '$page', '$timenow')"; 
        $viewer_new_result = mysql_query($viewer_new_query); 
    } 

//Get the total number of viewers for this page 
$viewer_total_query = "SELECT * FROM counter WHERE category = '$category' AND page = '$page'"; 
$viewer_total_result = mysql_query($viewer_total_query); 
$viewer_total_numrows = mysql_num_rows($viewer_total_result);
 
?>
<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
		 &gt; <a href="index.php?action=news" title='<?php echo translate('news'); ?>'><?php echo translate('news'); ?></a> 
         &gt; <a href="<?php echo $url_to_post; ?>"><?php echo $NEWS['title']; ?></a>
</div>
         
<div class="post">
	<h2><a class="title" href="<?php echo $url_to_post; ?>"><?php echo $NEWS['title']; ?></a></h2>
	<div class="info">
    
		<span class="date"><?php echo formatDate($NEWS['date'], $dateformat); ?></span>
        <?php 
		if($NEWS['date_update'] != 0)
		{
		?>
        <span class="date"><?php echo ucf(translate('updated')) . " " . translate('for2') . " " . getHowLongAgo($NEWS['date_update']); ?> </span>
        <?php
		}
		?>
        
		<span class="author"><a href="index.php?action=profile&id=<?php echo $U_PROFILE->getId(); ?>" title="<?php echo translate('postby'); ?> <?php echo ucf($U_PROFILE->getFirstname()) . " " . ucf($U_PROFILE->getLastname()); ?>"><?php echo ucf($U_PROFILE->getFirstname()) . " " . ucf($U_PROFILE->getLastname()); ?></a> <?php echo " " . $viewer_total_numrows . " "; if($viewer_total_numrows == 1) { echo translate('oneview'); } else { echo translate('views'); } ?></span>        
<?php
if(isset($USER) && $USER->getId() == $U_PROFILE->getId() or $USER->getRank() > 5)
{
	?>
				<!-- Only for admins and blog owner-->
        <span class="editpost"><a href="index.php?action=news&sub=editn&nid=<?php echo $NEWS['id']; ?>" title="<?php echo translate('editp_sub'); ?>"><?php echo translate('editp'); ?></a></span>
	<?php
}
?>
		<span class="comments"><a href="#comments" title="<?php echo translate('gotocomments'); ?>"><?php echo translate('gotocomments'); ?></a></span>
		<div class="fixed"></div>
		<div class="content">

		<?php
        echo $NEWS['comment'];
        ?>
		</div>
		<div class="fixed"></div>
	</div>
<!-- Show single blog post END-->
<?php
if(checkSession() && ($USER->getId() == $NEWS['user_id']))
{
	$but_text = translate('disable_commenting');
	if($NEWS['disable_comment'] == 1)
	{
		$but_text = translate('enable_commenting');
	}
	?>
	<form method="post" action="<?php echo $url_to_post; ?>">
		<input style="font-weight:bold;" type="submit" name="toggle_commenting" value="<?php echo $but_text; ?>" />
	</form>	
	<?php
}

$total_rating_text = "";
$total_rating = 0;
$raters_count = 0;
foreach($comments as $k => $v)
{
	if($v['rating'] >= 0)
	{
		$total_rating += $v['rating'];
		$raters_count++;
	}
}

if($raters_count > 0)
{
	$rating_score = ($total_rating / $raters_count);
	$rating_score = round($rating_score, 1);
	$total_rating_text = translate('rating_score');
	$total_rating_text = str_replace('VAR1', $rating_score, $total_rating_text);
}

?>
<!-- comments START -->
	<div id="comments">
		<div id="cmtswitcher">
			<a id="commenttab" class="curtab" href="javascript:void(0);" >
			<?php
			if(count($comments) == 0)
			{
			?>
			<?php echo ucf($comments_text0); ?>
			<?php
			}
			?>
			 <?php
			if(count($comments) == 1)
			{
			?>
			<?php echo $comments_text1; ?>
			<?php
			}
			?>
			<?php
			if(count($comments) >= 2)
			{
			?>
			<?php echo $comments_text2; ?>
			<?php
			}
			?>
            <?php
            if($total_rating_text != "")
			{
			?>
			<?php echo ", " . $total_rating_text; ?>
            <?php } ?></a>
            
            <?php 
			if($NEWS['category'] != "")
			{
				?>
				<span class="categories"><?php echo translate('category'); ?>: <a href="index.php?action=news&cat=<?php echo $NEWS['category']; ?>">
				<?php 
				if($NEWS['category'] == 'uncategorized')
				{
					echo translate($NEWS['category']);
				}
				else
				{
					echo $NEWS['category'];
				}
				?>
				</a></span>
				<?php
            }
            ?>
            
			<span class="addcomment"><a href="#respond"><?php echo translate('write_comment'); ?></a></span>
			<div class="fixed"></div>
		</div>
		<div id="commentlist">
			<ol id="thecomments">   
<?php
$counter = count($comments) + 1;
$counter2 = 0;
$showing = 0;

foreach($comments as $key => $val)
{
	if($counter2 >= $start && $counter2 < $end)
	{
    $COMMENT = $val;
    $counter--;
    include('comments_news.php');
	$showing++;
	}
	$counter2++;
}
?>
<?php 
if($counter2 == 0)
{
?>
<li class="messagebox"><?php echo translate('no_comments_yet');?></li>
<?php
}
?>
        	</ol>
   		</div>
	</div>
<!-- comments END -->

</div>
<?php
if($NEWS['disable_comment'] != 1)
{
	$post_id = $NEWS['id'];
	$post_type = 'news';
	$post_url = $url_to_post;
	include('comments_new_news.php');

	}
	else
	{
		echo "<br /><br />".
		"<h3>" . translate('commenting_disabled') . "</h3>";
	}

}
else
{
?>
<!-- Show all blog post START-->
<div class="post">
	<h2><a class="title" href="<?php echo $url_to_post; ?>" rel="bookmark"><?php echo $NEWS['title']; ?></a></h2>
	<div class="info">
    
		<span class="date"><?php echo formatDate($NEWS['date'], ''); ?></span>
        
		<span class="author"><a href="index.php?action=profile&id=<?php echo $U_PROFILE->getId(); ?>" title="<?php echo translate('postby'); ?> <?php echo ucf($U_PROFILE->getFirstname()) . " " . ucf($U_PROFILE->getLastname()); ?>"><?php echo ucf($U_PROFILE->getFirstname()) . " " . ucf($U_PROFILE->getLastname()); ?></a></span>
<?php
if(isset($USER) && $USER->getId() == $U_PROFILE->getId() or $USER->getRank() > 5)
{
	?>
				<!-- Only for admins and blog owner-->
        <span class="editpost"><a href="#" title="<?php echo translate('editp_sub'); ?>"><?php echo translate('editp'); ?></a></span>
	<?php
}
?>
        
        
		<span class="comments"><a href="<?php echo $url_to_post; ?>#comments" title="<?php echo $NEWS['title']; ?>"><?php echo $comments_text; ?></a></span>
        
		<div class="fixed"></div>		
		<div class="fixed"></div>
	</div>
</div>
<!-- Show all blog post END-->
<?php
}
?>
<!-- Display pagenumber START -->
    <center>
    	<?php
    	$post_text = $LANG['showing_comment'];
    	$post_text = str_replace('VAR1', $showing, $post_text);
    	$post_text = str_replace('VAR2', count($comments), $post_text);
    	
      if(count($comments) >  $USER->getCommentsPerPage())
      {
		echo $post_text . "<br />";
        displayPaging(count($comments), $USER->getCommentsPerPage(), $url_to_post, 'p', '<b>[', ']</b>'); 
     	}
      ?>
    </center>
    <!-- Display pagenumber END -->